//
//  GoalAndTimerViewController.swift
//  whack-a-mole
//
//  Created by wxc on 2019/9/20.
//  Copyright © 2019 wxc. All rights reserved.
//

import UIKit

class GoalAndTimerViewController: UIViewController {

    var dataArr :NSArray?
    var numLab = UILabel.init()
    var allnum = 0
    
    var dataArr2 :NSArray?
    var dataArr3 :NSArray?
    var numLab1 = UILabel.init()
    var allnum1 = 0
    override func viewDidLoad() {
        super.viewDidLoad()
        initUI()
        
        let numbers = UserDefaults.standard.object(forKey: GoalKey) as? NSNumber
        allnum = numbers?.intValue ?? 10
        let numbers1 = UserDefaults.standard.object(forKey: TimeKey) as? NSNumber
        allnum1 = numbers1?.intValue ?? 20
        
        let  titleLab = UILabel.init(frame: CGRect(x:0, y:100, width:self.view.frame.size.width, height:20))
        titleLab.text = "SET A GOAL"
        titleLab.textAlignment = NSTextAlignment.center
        self.view.addSubview(titleLab)
        
        numLab.frame=CGRect(x:0, y:titleLab.zc_bottom + 20, width:self.view.frame.size.width, height:40)
        numLab.textAlignment = NSTextAlignment.center
        numLab.font = UIFont.systemFont(ofSize: 50)
        numLab.text = String(allnum)
        self.view.addSubview(numLab)
        self.dataArr = ["-20", "-15","-10","-5","Rest","+5", "+10","+15","+20",]
        
        let w  = Float(self.view.frame.size.width/9)
        for i in 0...self.dataArr!.count - 1{
            let aw =  Int(w) * i
            let button = UIButton(type:.custom)
            button.frame = CGRect(x: aw, y:Int(numLab.zc_bottom) + 40, width:Int(w), height:30)
            button.setTitle(self.dataArr?[i] as? String, for: .normal)
            button.tag = 70 + i
            button.setTitleColor(UIColor.lightGray, for: .normal)
            button.layer.borderColor = UIColor.lightGray.cgColor
            button.layer.borderWidth = 0.5
            button.addTarget(self, action: #selector(buttonClick(button:)), for: .touchUpInside)
            self.view.addSubview(button)
        }
     
        let  titleLab1 = UILabel.init(frame: CGRect(x:0, y:numLab.zc_bottom + 110, width:self.view.frame.size.width, height:20))
        titleLab1.text = "SET A TIMER"
        titleLab1.textAlignment = NSTextAlignment.center
        self.view.addSubview(titleLab1)
        
        numLab1.frame=CGRect(x:0, y:titleLab1.zc_bottom + 20, width:self.view.frame.size.width, height:40)
        numLab1.textAlignment = NSTextAlignment.center
        numLab1.font = UIFont.systemFont(ofSize: 50)
        numLab1.text = String(allnum1)
        self.view.addSubview(numLab1)
        
        self.dataArr2 = ["-10m", "-1m","-10s","Rest", "+10s","+1m","+10m"]
        self.dataArr3=["-600", "-60","-10","0", "+10","+60","+600"]
        let w1  = Float(self.view.frame.size.width/7)
        for i in 0...self.dataArr2!.count - 1{
            let aw =  Int(w1) * i
            let button = UIButton(type:.custom)
            button.frame = CGRect(x: aw, y:Int(numLab1.zc_bottom) + 40, width:Int(w1), height:30)
            button.setTitle(self.dataArr2?[i] as? String, for: .normal)
            button.tag = 70 + i
            button.setTitleColor(UIColor.lightGray, for: .normal)
            button.layer.borderColor = UIColor.lightGray.cgColor
            button.layer.borderWidth = 0.5
            button.addTarget(self, action: #selector(timerButtonClick(button:)), for: .touchUpInside)
            self.view.addSubview(button)
        }
     
        let button = UIButton(type:.custom)
        button.frame = CGRect(x: 20, y:numLab1.zc_bottom + 130, width:self.view.frame.size.width-40, height:40)
        
        button.setTitle("Save", for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 25)
        button.setTitleColor(UIColor.blue, for: .normal)
        button.layer.borderColor = UIColor.lightGray.cgColor
        button.layer.borderWidth = 0.5
        button.addTarget(self, action: #selector(Save(button:)), for: .touchUpInside)
        self.view.addSubview(button)
        
    }
    
    @objc func buttonClick(button:UIButton ){
        
        let numstr = self.dataArr?[button.tag-70] as? String
        let num:NSInteger = Int(numstr!) ?? 0
        
        if button.tag != 74 {
            if allnum >= 0 {
                if allnum + num  >= 0{
                    allnum = allnum + num
                }
            }
        }else{
            allnum = 0
        }
        
        numLab.text = String(allnum)
    }
    
    @objc func timerButtonClick(button:UIButton ){
        
        let numstr = self.dataArr3?[button.tag-70] as? String
        let num:NSInteger = Int(numstr!) ?? 0
     
        if button.tag != 73 {
            if allnum1 >= 0 {
                if allnum1 + num  >= 0{
                    allnum1 = allnum1 + num
                }
            }
        }else{
            allnum1 = 0
        }
        
        numLab1.text = String(allnum1)
    }
    
    @objc func Save(button:UIButton ){
        
        UserDefaults.standard.set(allnum1>0 ? allnum1 : 9999, forKey: TimeKey)
        UserDefaults.standard.set(allnum>0 ? allnum : 9999, forKey: GoalKey)
        UserDefaults.standard.synchronize()
        self.dismiss(animated: true, completion: nil)
    }
}

extension GoalAndTimerViewController {
    func initUI() -> Void {
        view.backgroundColor = UIColor.white
    }
}
